// 加载数据
const loader = new Load(
    axios.create({
        base: "/"
    })
)

let option = {
    el: "#app",
    data: {
        //左侧工程
        pros: [],
        //当前工程
        currentPro: "",
        currentProList:[],
        //当前slk
        currentSlkName: "",
        currentItemName:"",
        //当前field
        currentField: "",
        //fields模板列表
        fields: [],
        //属性
        currentSlk: [],
        prokeyword: "",
        fieldKeyword:"",
    },
    computed:{
        showFields(){
            if(this.fieldKeyword){
                return this.fields.filter(d => {
                    return d.name.includes(this.fieldKeyword)
                    || d.zh.includes(this.fieldKeyword)
                })
            }else{
                return this.fields;
            }
        }
    },
    created() {
        //加载左侧
        this.loadPros();
    },
   
    methods: {
        loadPros() {
            loader.loadPros((res) => {
                this.pros = res;
            });
        },
        searchPro() {
            if(!this.prokeyword){
                alert("请输入工程名称");
                return false;
            }
            //检查是否已创建
            let checkPro = this.pros.find(d => d.includes(this.prokeyword))
            if(!checkPro){
                alert("搜索失败");
                return false;
            }else{
                this.currentPro = checkPro;
            }
            console.log("this.pro",this.currentPro);
        },
        createPro() {
            if(!this.prokeyword){
                alert("请输入工程名称");
                return false;
            }
            //检查是否已创建
            let checkPro = this.pros.indexOf(this.prokeyword)
            if(checkPro > -1){
                alert("工程名称已存在,请输入其他名称");
                return false;
            }
            console.log("创建工程",this.prokeyword);
        },
        loadPro(proName){
            console.log("加载工程",proName);
            loader.loadPro(proName,(res)=>{
                console.log("工程信息",res);
                this.currentPro = proName;
                this.currentProList = res
            })
        },
        loadSlk(slkName){
            console.log("loadSlk",slkName);
            loader.loadSlk(this.currentPro,slkName,(res)=>{
                this.currentSlk = res ? res : [];
                this.currentSlkName = slkName;
            })
            loader.loadField(slkName,(res)=>{
                this.fields = res;
            })
        },
        createSlk(slkName){
            console.log("createSlk",slkName);
        },
        setCurrentItem(itemName){
            this.currentItemName = itemName;
        }
    },
}

new Vue(option);
